package engine.newUi
{
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.RoleVo;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.scene.SceneManager;
	import engine.tip.TipsMainLogic;
	import engine.ui.UIManage;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import newx.com.config.StaticIni;
	import newx.com.control.window.NxWindowEvent;
	
	public class StrideOtherWarfareLogic
	{
		private const BTN_LAB:String="Common_Look";
		private static var _self:StrideOtherWarfareLogic;
		public static function getInstance():StrideOtherWarfareLogic
		{
			if(_self==null)
			{
				_self=new StrideOtherWarfareLogic();
			}
			return _self;
		}
		private  var _ui:StrideOtherWarfareUI;
		public  function get ui():StrideOtherWarfareUI
		{
			if(_ui==null)
			{
				_ui=new StrideOtherWarfareUI();
				UIManage.getInstance().initUI(_ui,"StrideOtherWarfareUI");
			}
			return _ui;
		}
		public function StrideOtherWarfareLogic()
		{
		}
		private var _bind:BindManage;
		private var _model:ModelLocator
		private var _role:RoleVo;
		private var _tip:TipsMainLogic;
		/**
		 *第几个战场 
		 */		
		private var _index:int;
		
		private var _topType:int;
		
		public function  Init():void
		{
			ui;
			_bind=BindManage.getInstance();
			_model=ModelLocator.getInstance();
			_role=_model.scene.Role;
			_tip=TipsMainLogic.instance;
			
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_RISE_SITE_FIGHT_INFO),openUI);
			
		}
		private function initGroup():void
		{
			
		}
		/**
		 *放大镜返回
		 //战场战斗记录
		 SERVER_CUSTOMMSG_RISE_SITE_FIGHT_INFO = 200473
		 1. fighttype
		 2. fightSite
		 3. leftPlayerName
		 4. leftPlayerScore
		 5. rightPlayernamme
		 6. rightPlayerScore
		 7. wheelAmt (一共多少轮)
		 8 数组(每轮的胜利玩家)
		 
		 * @param e
		 * 
		 */		
		private function openUI(e:CustomMsgEvent):void
		{
			trace("StrideOtherWarfareLogic.openUI(e)",e.pList);
			removeAll();
			_topType=e.pList[0];
			_index=e.pList[1];
			var rightS:int=e.pList[5];
			var leftS:int=	e.pList[3]
			changeTitle(e.pList[2],e.pList[4],leftS+":"+rightS);
			var len:int=e.pList[6];
			for (var i:int = 0; i < len; i++) 
			{
				changeRecord(i,e.pList[7+i])
			}
			if(rightS!=3&&leftS!=3)
			{
				changeRecord(i,"");
			}
			if(!_ui.created)
			{
				_ui.showOrHide();
			}
		}
		public function	BeforeShow():void
		{
			addAllEventListener()
		}
		public function	AfterHide():void
		{
			removeAllEventListener();
		}
		public function addAllEventListener():void
		{
			_ui.windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE,close);
			_ui.mainGroup.addItemsEvent(MouseEvent.CLICK,itemClickHandle);
			_ui.closebtn.addEventListener(MouseEvent.CLICK,close);
		}
		private function itemClickHandle(e:Event):void
		{
			var mc:MovieClip=e.currentTarget as MovieClip;
			if(e.target==mc["btn"]) 
			{
				trace("StrideOtherWarfareLogic.itemClickHandle(e)");
				//				StrideMainLogic.getInstance().ui.hide();
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_REQUEST_RISE_REPORT,_topType,_index,int(mc["index"]));
				//				SceneManager.getInstance().hideSceneView();
			}
		}
		/**
		 * 
		 * @param index  那个战场
		 * @param j			第几场战斗
		 * 
		 */		
		private function viewFight(j:int):void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_REQUEST_RISE_PLAYER,_topType,_index,j);
		}
		public function removeAllEventListener():void
		{
			_ui.windowBG.removeEventListener(NxWindowEvent.WINDOW_CLOSE,close);
			_ui.closebtn.removeEventListener(MouseEvent.CLICK,close);
		}
		private function removeAll():void
		{
			var len:int=_ui.mainGroup.row;
			for (var i:int = 0; i < len	; i++) 
			{
				clearItem(i);
			}
			
		}
		private function clearItem(index:int):void
		{
			var mc:MovieClip=_ui.mainGroup.getItemByIndex(index);
			mc["txt_0"].text="";
			mc["btn"].visible=false;
			mc["txt_1"].text="";
			//			mc["txt_2"].text="";
		}
		private function changeRecord(index:int,name:String):void
		{
			var mc:MovieClip=_ui.mainGroup.getItemByIndex(index);
			index+=1;
			if(name)
			{
				mc["txt_0"].htmlText=StaticIni.getSwfLangStrVar("StrideWarfare_"+index,[name])//"第"+index+"轮";
				mc["btn"].visible=true;
				mc["txt_1"].text=StaticIni.getSwfLangStr(BTN_LAB);
			}
			else
			{
				mc["txt_0"].htmlText=StaticIni.getSwfLangStr("StrideWarfare_6")//"比武中";
				mc["btn"].visible=false;
				mc["txt_1"].text="";
			}
			//			mc["txt_2"].text=name+"获胜";
			//			mc["txt_1"].text=StaticIni.getSwfLangStr(BTN_LAB);
			mc["txt_1"].mouseEnabled=false;
			mc["index"]=index;
		}
		private function changeTitle(leftFighter:String,rightFighter:String,result:String):void
		{
			_ui.title["txt"].htmlText=StaticIni.getSwfLangStrVar("UIString_1000606",[leftFighter,result,rightFighter]);
		}
		private function close(e:Event=null):void
		{
			_ui.hide();
		}
	}
}